package com.example.live.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatDialog
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.example.live.R
import com.example.live.adapter.RobAdapter
import com.example.live.entity.RobEntity

/**
 * 自定义红包对话框
 */
class  RedEnvelopeDialog: AppCompatDialog {
    constructor(context: Context): super(context)
    constructor(context: Context, themeResId: Int): super(context, themeResId)
    constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener?): super(context, cancelable, cancelListener)
    private var time = 0L
    fun setTime(time: Long) {
        this.time = time
    }
    private val list = mutableListOf<RobEntity>()
    private val robAdapter = RobAdapter()

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        list.add(RobEntity("张三",50))
        list.add(RobEntity("李四",40))
        list.add(RobEntity("王五",30))
        list.add(RobEntity("用户7435",20))
        list.add(RobEntity("钱七",20))
        list.add(RobEntity("赵立华",20))
        list.add(RobEntity("赵六",5))
        list.add(RobEntity("用户8665",5))
        list.add(RobEntity("用户9764",5))
        list.add(RobEntity("用户7685",5))
        robAdapter.data.addAll(list)
        //加载布局
        setContentView(R.layout.red_envelope_dialog_layout)
        val downView = findViewById<RedCircleCountDownView>(R.id.rcd)
        downView?.setCountDownTime(time.toInt())
        downView?.startCountDown()
        val prize = findViewById<ConstraintLayout>(R.id.rob_prize_layout)
        val bottomImage = findViewById<ImageView>(R.id.rob_red_envelope_bottom)
        val bottomText = findViewById<TextView>(R.id.rob_look_fortune)
        val robLayout = findViewById<LinearLayout>(R.id.rob_layout)
        val listLayout = findViewById<LinearLayout>(R.id.list_layout)
        val moneyLayout = findViewById<LinearLayout>(R.id.money_layout)
        val robRv = findViewById<RecyclerView>(R.id.rob_rv)
        downView?.setOnClickListener {
            if(downView.isFinish){
                prize?.visibility = View.VISIBLE
                bottomImage?.visibility = View.VISIBLE
                bottomText?.visibility = View.VISIBLE
            }else{
                ToastUtils.showShort("还未开抢")
            }
        }
        robRv?.adapter = robAdapter
        robRv?.layoutManager = LinearLayoutManager(context)
        bottomText?.setOnClickListener {
            robLayout?.visibility = View.GONE
            listLayout?.visibility = View.VISIBLE
            bottomImage?.visibility = View.GONE
            moneyLayout?.visibility = View.GONE
            downView?.visibility = View.GONE
            bottomText.visibility = View.GONE
        }

        //取消事件
        findViewById<ImageView>(R.id.red_close)?.setOnClickListener{
            cancel()
            downView?.cancelCountDown()
        }

        val windowParams: WindowManager.LayoutParams = window?.attributes!!
        windowParams.apply {
            dimAmount = 0.5f //设置灰暗度
            width = WindowManager.LayoutParams.WRAP_CONTENT
            height = WindowManager.LayoutParams.WRAP_CONTENT
        }
        window?.attributes = windowParams
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        setCancelable(true)
        setCanceledOnTouchOutside(true)
    }

}